package system.controller;

import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Record;
import common.info.JsonBackInfo;
import core.jfinal.ext.autoroute.ControllerBind;
import core.jfinal.ext.controller.BaseController;
import core.toolbox.ID.IdUtils;
import core.toolbox.Period.PeriodUtils;
import system.service.KMYEBService;
import system.service.UserSubjectBalanceService;
import system.service.VoucherService;

import java.util.*;


@ControllerBind(controllerKey = "voucher", viewPath = "")
public class VoucherController extends BaseController {
    VoucherService voucherService = Duang.duang(VoucherService.class);
    KMYEBService kmyebService = Duang.duang(KMYEBService.class);
    UserSubjectBalanceService userSubjectBalanceService = Duang.duang(UserSubjectBalanceService.class);

    /**
     * 获取记账凭证
     */
    public void get() {
        //获取会计周期
        String account_period = getPara("account_period");
        if (account_period == null)
            account_period = String.valueOf(PeriodUtils.getCurrentPeriod());
        //查询记账凭证记录
        List<Record> voucherList = voucherService.getVoucherList(account_period);
        //返回数据
        voucherList = (List<Record>) resolveInnerRecord(voucherList);
        setAttr("voucherList", voucherList);
        //返回视图
        render("/page/VoucherList.ftl");

    }

    /**
     * 添加记账凭证
     */
    public void insert() {
        JsonBackInfo backInfo = new JsonBackInfo();
        //获取前端数据
        Map paraMap = getParaMap();
        //处理数据
        paraMap = resolveInsertData(paraMap);
        //生成凭证编号
        int jzpzId = IdUtils.getJzpzId();
        //封装到Record
        Record record = new Record().setColumns(paraMap);
        //记录记账凭证
        voucherService.insertJzpz(jzpzId, record);
        //记录到用户科目数据
        userSubjectBalanceService.insert(record);
        backInfo.success();


        renderJson(backInfo);
    }

    /**
     * 更新记账凭证
     */
    public void update() {
        JsonBackInfo backInfo = new JsonBackInfo();
        //获取前端数据
        Map paraMap = getParaMap();
        int voucher_id = Integer.valueOf((String) paraMap.get("voucher_id"));
        //删除统计数据
        userSubjectBalanceService.deleteUserBalanceRecordByVoucherId(voucher_id);
        //删除记账凭证
        voucherService.deleteVoucherByVoucherId(voucher_id);
        //处理插入数据
        paraMap = resolveInsertData(paraMap);
        //封装到Record
        Record record = new Record().setColumns(paraMap);
        //插入统计数据
        userSubjectBalanceService.insert(record);
        //插入记账凭证
        voucherService.insertJzpz(voucher_id, record);

        renderJson(backInfo.success("更新成功"));

    }

    /**
     * 删除记账凭证
     */
    public void delete() {
        JsonBackInfo backInfo = new JsonBackInfo();
        //获取前端数据
        int voucher_id = Integer.valueOf(getPara("voucher_id"));
        //删除统计数据
        userSubjectBalanceService.deleteUserBalanceRecordByVoucherId(voucher_id);
        //删除记账凭证
        voucherService.deleteVoucherByVoucherId(voucher_id);
        //判断操作是否成功
        backInfo.success("删除成功");

        renderJson(backInfo);
    }


    /**
     * 处理前端传输的数据
     *
     * @param paraMap
     */
    private Map resolveInsertData(Map paraMap) {
        String[] voucher_digest = (String[]) paraMap.remove("voucher_digest");
        String[] subject_name = (String[]) paraMap.remove("subject_name");
        String[] voucher_debit = (String[]) paraMap.remove("voucher_debit");
        String[] voucher_credit = (String[]) paraMap.remove("voucher_credit");
        List voucher_details = new LinkedList();
        paraMap.put("voucher_detail", voucher_details);
        for (int i = 0; i < voucher_digest.length; i++) {
            String str = voucher_digest[i];
            if (str != null && str.trim().length() > 0) {
                Record record = new Record();
                record.set("voucher_digest", voucher_digest[i]);
                record.set("subject_name", subject_name[i]);
                record.set("voucher_debit", voucher_debit[i]);
                record.set("voucher_credit", voucher_credit[i]);
                voucher_details.add(record);
            }
        }

        return paraMap;
    }


}
